﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media.TextFormatting;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Collections.Specialized;
using SDF.Primitives;
using SDF.TextFormatting;
using System.Collections;
using System.ComponentModel;
using SDF.Pagination;

namespace SDF
{
    /// <summary>
    /// Used in harvesting, to return a "frozen" rendered SmartBlock
    /// </summary>
    public class FrozenBlock : SmartBlock
    {
        #region Constructor

        public FrozenBlock()
        {
            Canvas = new Canvas();
            Canvas.Height = 0;
            Canvas.Width = 0;
        }

        #endregion

        #region Properties

        public UIElementCollection Children
        {
            get { return Canvas.Children; }
        }

        protected Canvas Canvas { get; private set; }

        public double Width
        {
            get { return Canvas.Width; }
            set { Canvas.Width = value; }
        }

        public double Height
        {
            get { return Canvas.Height; }
            set { Canvas.Height = value; }
        }

        #endregion

        #region Visual Children Implementation

        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= 1)
                throw new ArgumentOutOfRangeException("index");

            return Canvas;
        }

        #endregion

        #region Measure and Arrange Methods

        protected override Size MeasureOverride(Size availableSize)
        {
            Canvas.Measure(availableSize);
            return Canvas.DesiredSize;
        }

        protected override Size ArrangeOverride(Size availableSize)
        {
            Canvas.Arrange(new Rect(availableSize));
            return availableSize;
        }

        #endregion

        #region Not Implemented Methods

        public override HarvestResult Harvest(HarvestingContext context, double remainingHeight, SmartBlock previousBlock)
        {
            throw new NotImplementedException("FrozenBlocks should not be harvested. They should instead, be returned as the result of a harvesting. Check your code.");
        }

        public override SmartBlock GetEmptyCopy()
        {
            throw new NotImplementedException("FrozenBlocks should not be harvested. They should instead, be returned as the result of a harvesting. Check your code.");
        }

        public override double CalculateMinHeight()
        {
            throw new NotImplementedException("FrozenBlocks should not be harvested. They should instead, be returned as the result of a harvesting. Check your code.");
        }

        #endregion

        #region Property Changed

        protected override void OnPropertyChanged(string propertyName)
        {
            base.OnPropertyChanged(propertyName);

            if (propertyName == "Background")
                Canvas.Background = this.Background;
        }

        #endregion
    }
}
